
import {m_jdbc2one,m_jdbc2many} from './jdbc'
import {Monad} from './monad'
import md5 from '../utils/md5'
import moment from 'moment'

/**
 * 发布一条 交易头寸信息。
 * @param {*} sender 发送方
 * @param {*} receiver 接收方
 * @param {*} start_time 发布时间
 * @param {*} due_time 结束时间
 * @param {*} side 头寸位置
 * @param {*} underlying 标的名称
 * @param {*} price 价格
 * @param {*} place_Of_deliver 交货地
 * @param {*} reference 引用id
 */
function post_to_trade_position(sender,receiver,start_time,due_time,side,underlying,price,place_Of_deliver,reference){
    const sql = `insert into T_TRADE_POSITION(Sender,Receiver,StartTime,DueTime,Side,Underlying,Price,PlaceOfDeliver,Reference
        values('${sender}','${receiver}','${start_time}','${due_time}','${side}','${underlying}','${price}','${place_Of_deliver}','${reference}'))`
    m_jdbc2one(sql).bind(data=>{
        console.log(data);
    })
}

/**
 * 待保存到数据库中的数据对象
 * @param {*} obj 数据对象
 */
function post_to_original_quotation(obj){
    let sql = obj2insert_sql(obj,"T_ORIGINAL_QUOTATION");// 生成原始报价的数据记录
    console.log(sql);
    m_jdbc2one(sql).bind(data=>{
        console.log(data);
    })
}

/**
 * 把一个对象转换成 insert sql
 * @param {*} obj 
 * @param {*} TABLE_NAME 
 */
function obj2insert_sql(obj,TABLE_NAME){
    const keys = Object.keys(obj);// 字段信息
    const values = keys.map(key=>`'${obj[key]}'`);// 字段值信息
    const sql = `insert into ${TABLE_NAME}(${keys.join(",")}) values (${values}) `;
    return sql;
}

/**
 * 文本消息挂钩
 * @param {*} param0 发送上下文
 * @param {*} obj 消息对象
 */
function imt_text_msg_hook({state, commit}, obj){
    const msg = {sender:state.userUID,receiver:obj.to,start_time:new Date(),text:obj.text,scene:obj.scene,type:obj.type};
    msg.md5 = md5(msg.text);
    const ss = msg.text.split(/[\s,]+/);
    let i = 0;
    var quotation={};
    ss.forEach(s=>{
        switch(i){
            case 0:{// Underlying
                quotation['Underlying']=s;
                break;
            }
            case 1:{// Side
                quotation['Side']=s;
                break;
            }
            case 2:{// Date
                quotation['DeliveryDate']=s;
                break;
            }
            case 3:{// Quantity
                quotation['Quantity']=s;
                break;
            }
            case 4:{// Price
                quotation['Price']=s;
                break;
            }
            case 5:{// PlaceOfDeliver
                quotation['PlaceOfDeliver']=s;
                break;
            }
            case 6:{// Qualification
                quotation['Qualification']=s;
                break;
            }
            case 7:{// PublishTime
                quotation['PublishTime']=s;
                break;
            }
        }
        i = i+1;
    });
    quotation['md5'] = msg.md5;// 加入 md5的数据记录
    quotation['Publisher'] = state.userUID;// 加入 md5的数据记录
    quotation['PublishTime'] = moment(new Date()).format("YYYY-MM-DD hh:mm:ss.SSS");;// 加入 md5的数据记录
    quotation['Receiver'] = msg.receiver;// 加入 md5的数据记录
    post_to_original_quotation(quotation);
}

exports.post_to_trade_position = post_to_trade_position;
exports.imt_text_msg_hook = imt_text_msg_hook;